#pragma once
#include<iostream>
#include<assert.h>

using namespace std;

namespace Vector
{
	template<class T>
	class vector 
	{
	public:
		typedef T* iterator;
		typedef const T* const_iterator;
		vector()
		{}

		vector(size_t n, const T& val = T())
		{
			reserve(n);
			while (n > 0)
			{
				push_back(val);
				n--;
			}
		}

		template <class InputIterator>
		vector(InputIterator first, InputIterator last)
		{
			while (first != last)
			{
				push_back(*first);
				++first;
			}
		}


		vector(int n, const T& val = T())
		{
			reserve(n);
			while (n > 0)
			{
				push_back(val);
				n--;
			}
		}


		vector(const vector<T>& x)
		{
			reserve(x.capacity());
			for (auto& e : x)
			{
				push_back(e);
			}
		}

		void swap(vector<T>& tmp)
		{
			std::swap(_start = tmp._start);
			std::swap(_finish = tmp._finish);
			std::swap(_endofstorage = tmp._endofstorage);
		}

		vector<T>& operator=(vector<T> tmp)
		{
			swap(tmp);
			return *this;
		}


		iterator begin()
		{
			return _start;
		}

		iterator end()
		{
			return _finish;
		}

		const_iterator begin() const
		{
			return _start;
		}

		const_iterator end() const
		{
			return _finish;
		}

		size_t size() const
		{
			return _finish - _start;
		}

		size_t capacity() const
		{
			return _endofstorage - _start;
		}

		void reserve(size_t n)
		{
			if(_start + n >= _endofstorage)
			{ 
				size_t sz = size();
				T* tmp = new T[n];
				if (_start)
				{
					for (size_t i  = 0; i < n;i++)
					{
						tmp[i] = _start[i];
					}

					delete[] _start;
				}
				_start = tmp;
				_finish = _start + sz;
				_endofstorage = _start + n;
			}
		}

		void resize(size_t n, const T& val = T())
		{
			if (n <= size())
			{
				_finish = _start + n;
			}
			else
			{
				reserve(n);
				while (_finish != _start + n)
				{
					push_back(val);
				}
			}
		}

		void push_back(const T& x)
		{
			if (_finish == _endofstorage)
			{
				reserve(capacity() == 0 ? 4 : capacity() * 2);
			}

			*_finish = x;
			++_finish;
		}

		iterator insert(iterator pos, const T& val = T())
		{
			assert(pos <= _finish);
			assert(pos >= _start);
			size_t len = pos - _start;
			if (_finish == _endofstorage)
			{
				reserve(capacity() == 0 ? 4 : capacity() * 2);
			}
			pos = _start + len;
			iterator end = _finish;
			while (end > pos)
			{
				*end = *(end-1);
				end--;
			}
			*pos = val;
			++_finish;
			return pos;
		}

		iterator erase(iterator pos)
		{
			assert(pos < _finish);
			assert(pos >= _start);

			iterator begin = pos;
			while (pos < _finish)
			{
				*pos = *(pos + 1);
				pos++;
			}
			--_finish;
			return pos;
		}

		T& operator[](const T& x)
		{
			return _start[x];
		}

		const T& operator[](const T& x) const
		{
			return _start[x];
		}

		~vector()
		{
			delete[] _start;
			_start = _finish = _endofstorage = nullptr;
		}
	private:
		iterator _start = nullptr;
		iterator _finish = nullptr;
		iterator _endofstorage = nullptr;
	};

	/*void test_vector3()
	{
		vector<int> v1;
		v1.push_back(1);
		v1.push_back(2);
		v1.push_back(3);
		v1.push_back(4);
		v1.push_back(5);
		v1.push_back(6);
		v1.push_back(7);
		v1.push_back(8);
		v1.push_back(9);
		v1.push_back(10);

		string s1("hello world");
		vector<int> v2(s1.begin(), s1.end());
		vector<int> v3(10,59);
		for (auto& ch : v2)
		{
			cout << ch << " ";
		}
		cout << endl;
		
	}


	void test_vector2()
	{
		vector<int> v1;
		v1.push_back(1);
		v1.push_back(2);
		v1.push_back(3);
		v1.push_back(4);
		v1.push_back(5);
		v1.push_back(6);
		v1.push_back(7);
		v1.push_back(8);
		v1.push_back(9);
		v1.push_back(10);

		for (auto& ch : v1)
		{
			cout << ch << " ";
		}
		cout << endl;

		v1.insert(v1.begin()+3,888);
		for (auto& ch : v1)
		{
			cout << ch << " ";
		}
		cout << endl;

		v1.erase(v1.begin() + 3);
		for (auto& ch : v1)
		{
			cout << ch << " ";
		}
		cout << endl;

		v1.insert(v1.end(), 888);
		for (auto& ch : v1)
		{
			cout << ch << " ";
		}
		cout << endl;

		v1.erase(v1.end()-1);
		for (auto& ch : v1)
		{
			cout << ch << " ";
		}
		cout << endl;

		v1.insert(v1.begin(), 888);
		for (auto& ch : v1)
		{
			cout << ch << " ";
		}
		cout << endl;

		v1.erase(v1.begin());
		for (auto& ch : v1)
		{
			cout << ch << " ";
		}
		cout << endl;
	}


	void test_vector()
	{
		vector<int> v1;
		v1.push_back(1);
		v1.push_back(2);
		v1.push_back(3);
		v1.push_back(4);
		v1.push_back(5);
		v1.push_back(6);
		v1.push_back(7);
		v1.push_back(8);
		v1.push_back(9);
		v1.push_back(10);

		vector<int>::iterator it = v1.begin();
		while (it != v1.end())
		{
			cout << *it << " ";
			++it;
		}
		cout << endl;

		for (auto& ch : v1)
		{
			cout << ch << " ";
		}
		cout << endl;

		for (int i = 0; i < 10; i++)
		{
			cout << v1[i] << " ";
		}
		cout << endl;
	}

	void test_vector1()
	{
		vector<int> v1;
		v1.push_back(1);
		v1.push_back(2);
		v1.push_back(3);
		v1.push_back(4);
		v1.push_back(5);
		v1.push_back(6);
		v1.push_back(7);
		v1.push_back(8);
		v1.push_back(9);
		v1.push_back(10);

		for (auto& ch : v1)
		{
			cout << ch << " ";
		}
		cout << endl;

		v1.resize(20,999);

		for (auto& ch : v1)
		{
			cout << ch << " ";
		}
		cout << endl;
	}*/
}
